/*****************************************************************************
 * SerialUSB.c
 *
 * To flash the bootloader:
 * 1) Project > Project Options > Type: Linker File
 * 2) Rebuild Project
 * 3) Tools > Flash Programmer.. > Browse > 
 *        ..\ADSP-21369 EZ-KIT Lite\Flash Programmer\Parallel\21369EzFlashDriver_Parallel.dxe
 * 4) Hit Load
 * 5) Click programming tab and find the linker file. Click 'Program'
 * 6) Program will now boot on power-up
 *
 *****************************************************************************/

/*****************************************************************************
*
* This program echos back over the UART. If you send it data over the serial
* line, it will echo it back over serial. This program is useful for learning
* how the DSP talks to the computer for data transfers.
*
******************************************************************************/

#include <cdef21369.h>
#include <def21369.h>

//#define SRUDEBUG  // Check SRU Routings for errors.
#include <SRU.h>

int i=0;
/////////////////////////////
/*   Function Prototypes   */
/////////////////////////////

//Functions for talking via USB
void SetupUART(void);	//function for initializing the UART
void sendData(long);
void sendData2(long);

	
/*	Servo Pin Arrangements

  DPI Header
  
    --------
    --1--2--
    --3--4--
    --5--6--
    --7--8--
    --9--10-
    -11--12-
    -13--14-
    -15--16-
    -17--18-
    -19--20-
    --------
    
Servo Function / DPI Location
!!! Be Careful to assign correct functions to the correct
  locations otherwise you could damage the DACs/ADCs. Always
  remove the header and check with a oscilloscope first. 
  And Disalbe the pins you do not need !!!

1 GND				      	2 GND
3 DAC SDI 	    (DPI_01) 	4 ADC CLKR		(DPI_02)
5 DAC SCLK 	    (DPI_03)   	6 NC			(DPI_04)
7 NC	 	    (DPI_05)   	8 DAC 01 CS		(DPI_06)
9 3.3V				      	10 3.3V
11 DAC 02 CS    (DPI_07) 	12 DAC 03 CS	(DPI_08)
13 USBTX/UARTRX (DPI_09)   	14 USBRX/UARTTX	(DPI_10)
15 ADC DRR	    (DPI_11)   	16 ADC FSX		(DPI_12)
17 ADC CS	    (DPI_13) 	18 NC			(DPI_14)
19 3.3V				   		20 3.3V
	

*/

//////////////////////////
/*   Define Functions   */
//////////////////////////

/////////// UART FUNCTIONS for Serial-USB Communication ///////////
void SetupUART()
{
	// Assign Functions to Pins
	// Note USB TX should be connected to UART RX
	// and USB RX connected to UART TX
  	SRU2(UART0_TX_O,DPI_PB09_I); // UART transmit signal is connected to DPI pin 9
  	SRU2(DPI_PB10_O,UART0_RX_I); // connect the pin buffer output signal to the UART0 receive
  	SRU2(LOW,DPI_PB10_I);        // Set Pin Low for receive
  	
  	SRU2(HIGH,DPI_PBEN09_I);     // Enable Pin 10
  	SRU2(LOW,DPI_PBEN10_I);      // disables DPI pin 9 as input

  	
	/* Sets the Baud rate for UART0 */	
	*pUART0LCR = UARTDLAB;  //enables access to Divisor register to set baud rate
	
	*pUART0DLL = 0x2D;      //cclk=331.776MHz, UART baud = cclk/(2*16*divisor)
	*pUART0DLH = 0x00; 
	//*pUART0DLH = 0x12;
	// or cclk = 319.488 MHz
	// 0x05 sets a baudrate of 2.0 MBaud
	// cclk can be changed in initPLL to accomodate other baudrates
  
    /* Configures UART0 LCR */ 
    *pUART0LCR = 0;
    *pUART0LCR = UARTWLS8| 				// word length 8
                 UARTPEN| 				// parity enable ODD parity
                 UARTSTB|				// Two stop bits
                 UARTEPS|
                 UARTSP;
               
               
    *pUART0RXCTL = UARTEN;       //enables UART0 in receive mode

    *pUART0TXCTL = UARTEN;       //enables UART0 in core driven mode
 
}

void sendData(long val)
{
	*pUART0THR = 0xab; //add 1 char of data to the buffer
    while(!(*pUART0LSR & UARTTHRE)); //wait until it is sent
    *pUART0THR = 0xcd; //add 1 char of data to the buffer
    while(!(*pUART0LSR & UARTTHRE)); //wait until it is sent
    
	while(!(*pUART0LSR & UARTTHRE)); //make sure UART xmit buffer is ready
	for(i=3; i>=0; i--)
	{
    	*pUART0THR = val >> (8*i); //add 1 char of data to the buffer
    	while(!(*pUART0LSR & UARTTHRE)); //wait until it is sent
	}
}

void sendData2(long val)
{
	while(!(*pUART0LSR & UARTTHRE)); //make sure UART xmit buffer is ready
	for(i=3; i>=0; i--)
	{
    	*pUART0THR = val >> (8*i); //add 1 char of data to the buffer
    	while(!(*pUART0LSR & UARTTHRE)); //wait until it is sent
	}

}

